package com.lungle.warmdiary.app.content.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.lungle.warmdiary.app.AppConstant;
import com.lungle.warmdiary.app.content.db.helper.CacheSQLiteOpenHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by God on 2016/8/27.
 */
public class ImageUploadDAO extends BaseDAO {
    private CacheSQLiteOpenHelper helper;
    private String DB_NAME = AppConstant.CACHE_DB;


    public ImageUploadDAO(Context context) {
        this.helper = new CacheSQLiteOpenHelper(context, DB_NAME);
    }

    public boolean insert(String aid, String mid, String fileName) {
        db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("aid", aid);
        values.put("mid", mid);
        values.put("file_path", fileName);
        long result = db.insert("T_image_upload", null, values);
        close();
        return result == -1 ? false : true;
    }


    public void removeAll() {
        db = helper.getWritableDatabase();
        db.delete("T_image_upload", null, null);
        db.endTransaction();
    }


    public List<String> find(String aid, String mid, String fileName) {
        db = helper.getReadableDatabase();
        List<String> files = new ArrayList<>();
        Cursor cursor = null;
        try {
            cursor = db.rawQuery("select * from T_image_upload where aid=? and mid=? and file_path=?", new String[]{aid, mid, fileName});
            while (cursor.moveToNext()) {
                files.add(cursor.getString(cursor.getColumnIndex("file_path")));
            }
        } catch (Exception e) {
            e.printStackTrace();
            files = Collections.emptyList();
        } finally {
            if (cursor != null) cursor.close();
        }

        return files;
    }


}
